<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true">
      <el-form-item label="姓名" prop="name">
        <el-input
          v-model="queryParams.name"
          placeholder="请输入姓名"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="手机号" prop="phone">
        <el-input
          v-model="queryParams.phone"
          placeholder="请输入手机号"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item label="地级市" prop="city">
        <el-select
          v-model="queryParams.deptId"
          placeholder="请选择地级市"
          size="small"
          style="width: 240px"
        >
          <el-option
            v-for="city in citys"
            :key="city.deptId"
            :value="city.deptId"
            :label="city.deptName"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="部门" prop="deptName">
        <el-input
          v-model="queryParams.deptName"
          placeholder="请输入部门"
          clearable
          size="small"
          style="width: 240px"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button
          type="primary"
          icon="el-icon-search"
          size="mini"
          @click="handleQuery"
          >搜索</el-button
        >
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery"
          >重置</el-button
        >
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="warning"
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['system:post:export']"
          >导出</el-button
        >
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="bindingList">
      <!-- <el-table-column label="编号" prop="userId" /> -->
      <el-table-column label="姓名" prop="name" />
      <el-table-column label="手机号" prop="phone" />
      <el-table-column label="地级市" prop="city" />
      <el-table-column label="部门" prop="deptName" />
      <el-table-column label="职务" prop="post" />
      <el-table-column label="头像" prop="headImg">
        <template slot-scope="scope">
          <img :src="scope.row.headImg" alt="" class="headImg-ccc" />
        </template>
      </el-table-column>
      <el-table-column label="微信昵称" prop="nickName" />
      <el-table-column label="备注" prop="remark" />
      <el-table-column label="申请时间" prop="createTime"
        ><template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime) }}</span>
        </template></el-table-column
      >
      <el-table-column
        label="操作"
        align="center"
        class-name="small-padding fixed-width"
      >
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-edit"
            @click="modify(scope.row)"
            >编辑</el-button
          >
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total > 0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 修改信息 -->
    <el-dialog
      title="修改信息"
      :visible.sync="open"
      width="600px"
      :close-on-click-modal="false"
    >
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-row>
          <el-col :span="24">
            <el-form-item label="姓名" prop="name">
              <el-input v-model="form.name"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="手机号" prop="phone">
              <el-input v-model="form.phone"></el-input>
            </el-form-item>
          </el-col>
          <!-- <el-col :span="24">
            <el-form-item label="地级市" prop="city">
              <el-input v-model="form.city"></el-input>
            </el-form-item>
          </el-col> -->
          <el-col :span="24">
            <el-form-item label="职务" prop="post">
              <el-input v-model="form.post"></el-input>
            </el-form-item>
          </el-col>
          <el-col :span="24">
            <el-form-item label="备注">
              <el-input v-model="form.remark"></el-input>
            </el-form-item>
          </el-col>
        </el-row>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import {
  getUserBindingList,
  modifyUserBinding,
  exportExcel,
  getCitys,
} from "@/api/system/userbinding";

export default {
  name: "Role",
  data() {
    return {
      // 地级市列表
      citys: [
        {
          deptName: "全部",
          deptId: 0,
        },
      ],
      // 遮罩层
      loading: true,
      // 总条数
      total: 0,
      // 表格数据
      bindingList: [],
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 20,
        name: "",
        phone: "",
        // 这其实是地级市的ID
        deptId: 0,
        deptName: "",
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        name: [
          { required: true, message: "姓名不能为空", trigger: "blur" },
          { min: 2, max: 5, message: "长度在 2 到 5 个字符", trigger: "blur" },
        ],
        phone: [
          {
            required: true,
            min: 11,
            max: 11,
            message: "手机号长度为11位",
            trigger: "blur",
          },
        ],
        post: [
          { required: true, message: "职务不能为空", trigger: "blur" },
          { min: 2, message: "长度在大于 2 个字符", trigger: "blur" },
        ],
        email: [
          {
            type: "email",
            message: "'请输入正确的邮箱地址",
            trigger: ["blur", "change"],
          },
        ],
      },
      open: false,
    };
  },
  created() {
    this.getList();
    getCitys().then((response) => {
      if (response.code !== 200) return;
      this.citys = [...this.citys, ...response.data];
    });
  },
  methods: {
    /** 查询角色列表 */
    getList() {
      this.loading = true;

      getUserBindingList(this.queryParams).then((response) => {
        if (response.code !== 200) return;

        this.bindingList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 表单重置
    reset() {
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.queryParams = {
        pageNum: 1,
        pageSize: 10,
        name: "",
        phone: "",
        deptName: "",
      };
      this.getList();
    },

    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm("是否确认导出所有数据项?", "警告", {
        confirmButtonText: "确定",
        cancelButtonText: "取消",
        type: "warning",
      })
        .then(function () {
          // return exportRole(queryParams);
          return exportExcel();
        })
        .then((response) => {
          this.download(response.data.msg);
        })
        .catch(function () {});
    },

    /** 提交按钮 */
    submitForm: function () {
      this.$refs["form"].validate((ok) => {
        if (!ok) return;
        modifyUserBinding(this.form).then((res) => {
          this.open = false;
          this.getList();
        });
      });
    },

    /** 关闭弹窗 */
    cancel() {
      this.open = false;
      this.reset();
    },

    // 修改
    modify(row) {
      this.form = JSON.parse(JSON.stringify(row));
      this.open = true;
    },
  },
};
</script>
<style lang="scss">
.el-message {
  margin-top: 80px;
  z-index: 999999;
}

.el-dialog {
  display: flex;
  flex-direction: column;
  margin: 0 !important;
  position: absolute;
  top: 45%;
  left: 50%;
  transform: translate(-50%, -50%);
  /* height:600px; */
  max-height: calc(100% - 30px);
  max-width: calc(100% - 30px);
}

.el-dialog .el-dialog__body {
  flex: 1;
  overflow: auto;
}

.headImg-ccc {
  width: 40px;
}
</style>
